package com.xiangmo.travel.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Dto.NoticeConfirmDto;
import com.xiangmo.travel.Dto.NoticeDto;
import com.xiangmo.travel.Entity.Admin;
import com.xiangmo.travel.Entity.Attractions;
import com.xiangmo.travel.Entity.Notice;
import com.xiangmo.travel.Entity.TeamUser;
import com.xiangmo.travel.Service.AttractionsService;
import com.xiangmo.travel.Service.NoticeService;
import com.xiangmo.travel.mapper.AttractionsMapper;
import com.xiangmo.travel.mapper.NoticeMapper;
import com.xiangmo.travel.mapper.TeamRequestMapper;
import com.xiangmo.travel.mapper.TeamUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private TeamUserMapper teamUserMapper;
    @Autowired
    private TeamRequestMapper teamRequestMapper;
    @Autowired
    private AttractionsMapper attractionsMapper;

    @Override
    public int createNotice(NoticeDto noticeDto) {
        Notice notice = Notice.builder()
                .receiveUserId(noticeDto.getReceiveUserId())
                .sendUserId(noticeDto.getSendUserId())
                .teamId(noticeDto.getTeamId())
                .build();
        //标题
        String title1 = "申请结果";
        String title2 = "官方活动";
        String title3 = "消费提醒";
        String title4 = "申请请求";
        String title5 = "队伍通知";

        //内容：这里要判断通知的类型，是队长同意，还是队员申请
        String content1_title1 = "恭喜你！队长已同意你的入队申请,申请队名: < " + noticeDto.getTeamName() + " >";
        String content2_title1 = "很抱歉！队长拒绝了你的入队申请,申请队名: < " + noticeDto.getTeamName() + " >";
        String content1_title4 = "用户: " + noticeDto.getName() + " 申请加入您组织的 < " + noticeDto.getTeamName() + " > 队伍！ 留言信息:" + noticeDto.getTell();
        String content5 = "很遗憾，你已被请离队伍: < " + noticeDto.getTeamName() + " > ";
        String content6 = "您的队员:" + noticeDto.getName() + " 已经离开了您的队伍 < " + noticeDto.getTeamName() + " >";
        //返回状态码判断是哪种通知，进行拼接
        int status = noticeDto.getStatus();
        switch (status){
            //申请结果 1同意 2拒绝
            case 1:
                notice.setTitle(title1);
                notice.setContent(content1_title1);
                notice.setConfirm(0);
                break;
            case 2:
                notice.setTitle(title1);
                notice.setContent(content2_title1);
                notice.setConfirm(0);
                break;
             //申请请求
            case 3:
                notice.setTitle(title4);
                notice.setContent(content1_title4);
                notice.setConfirm(3);
                break;
            case 4:
                notice.setTitle(title5);
                notice.setContent(content5);
                notice.setConfirm(0);
                break;
            case 5:
                notice.setTitle(title5);
                notice.setContent(content6);
                notice.setConfirm(0);
                break;
            default:
                break;
        }
        log.info("生成通知标题：[{}]",notice.getTitle());
        log.info("生成通知内容：[{}]",notice.getContent());
        //操作数据库 生成数据
        return noticeMapper.insert(notice);
    }
    //通过用户ID查询通知
    @Override
    public Page<Notice> getNoticeByUserId(Long userId, int pageNum, int pageSize) {
        Page<Notice> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getReceiveUserId, userId);
        return noticeMapper.selectPage(page, lambdaQueryWrapper);
    }
    //修改通知的状态为已读
    @Override
    public int updateNoticeStatus(Long id) {
        Notice notice = new Notice();
        notice.setStatus(1);
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getId, id);
        return noticeMapper.update(notice, lambdaQueryWrapper);
    }
    //确认或拒绝成为组员的申请
    @Override
    public int updateNoticeConfirm(NoticeConfirmDto noticeConfirmDto) {
        Notice notice = new Notice();
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        int confirm = noticeConfirmDto.getConfirm();
        Long id = noticeConfirmDto.getId();
        Long userId = noticeConfirmDto.getUserId();
        Long teamId = noticeConfirmDto.getTeamId();
        int resoult = 0;
        TeamUser teamUser = TeamUser.builder()
                .teamId(teamId)
                .userId(userId)
                .build();
        notice.setId(id);
        notice.setStatus(1);
        if(confirm == 1){
            //接受
            notice.setConfirm(1);

            //增加组队表数据和删除请求表数据
            resoult += teamUserMapper.insert(teamUser);
            resoult += teamRequestMapper.deleteByTeamIdAndUserId(teamId, userId) >= 1 ? 2 : 0;
        }else if(confirm == 2){
            //拒绝
            notice.setConfirm(2);
            resoult += teamRequestMapper.deleteByTeamIdAndUserId(teamId, userId) >= 1 ? 2 : 0;
        }
        //先改状态
        noticeMapper.updateById(notice);
        // 返回 0 表示拒绝 返回 1 表示增加成员表成功其他没有操作 返回 2 删除请求表成功其他没有操作 返回 3 表示全部成功
        return resoult;
    }

    @Override
    public int publishedAttractions(NoticeDto noticeDto) {
        Admin admin = Admin.builder().build();
        Notice notice = Notice.builder()
                .receiveUserId(admin.getId()) //设置管理员id
                .sendUserId(noticeDto.getSendUserId())
                .title(noticeDto.getName())
                .confirm(3)
                .status(0)
                .build();
        //标题
        String title1 = "发布审核";
        String[] strArr = { "P", "Q", "R", "S","T", "U", "V", "W", "6", "4", "3", "2", "9", "8", "F", "C", "B"};
        Random rand = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for(int i=0; i<12; i++){
            int res = rand.nextInt(strArr.length);
            stringBuffer.append(strArr[res]);
        }
        //内容：这里要判断通知的类型，是队长同意，还是队员申请
        String content1_title1 = "用户发布景点: " + noticeDto.getName() + " 景点编号:" + stringBuffer.toString();
        notice.setTitle(title1);
        notice.setContent(content1_title1);
        notice.setTeamId(noticeDto.getTeamId());
        return noticeMapper.insert(notice);
    }

    @Override
    public int isPublishedAttractions(Long noticeId, Long attractionsId, Long userId, int status) {
        Attractions attractions = new Attractions();
        attractions.setId(attractionsId);
        attractions.setIsPublished(1);
        Attractions getAttractions = attractionsMapper.selectById(attractionsId);
        String content = "";
        Notice noticeA = Notice.builder().build();
        //拒绝
        if(status == 2){
            content = "您发布的景点文章 《" + getAttractions.getTitle() + "》 存在不规范！未能通过审核~ 如有异议或疑惑可联系客服: 18861xx1";
            attractions.setIsPublished(2);
        } else {
            content = "恭喜您！您发布的景点文章: 《" + getAttractions.getTitle() + "》 已通过审核，快去看看吧~";
            attractions.setIsPublished(1);
        }
        noticeA.setContent(content);
        noticeA.setTeamId(attractionsId);
        noticeA.setTitle("审核通知");
        noticeA.setReceiveUserId(userId);
        Admin admin = Admin.builder().build();
        noticeA.setSendUserId(admin.getId());
        noticeMapper.insert(noticeA);
        //设置确认情况
        Notice notice = new Notice();
        notice.setId(noticeId);
        notice.setStatus(1);
        notice.setConfirm(status);
        noticeMapper.updateById(notice);
        return attractionsMapper.updateById(attractions);
    }

    @Override
    public int deleteTeamUser(NoticeDto noticeDto) {
        Notice notice = Notice.builder()
                .receiveUserId(noticeDto.getReceiveUserId())
                .sendUserId(noticeDto.getSendUserId())
                .teamId(noticeDto.getTeamId())
                .build();
        //标题
        String title1 = "申请结果";
        String title2 = "官方活动";
        String title3 = "消费提醒";
        String title4 = "申请请求";

        //内容：这里要判断通知的类型，是队长同意，还是队员申请
        String content1_title1 = "恭喜你！队长已同意你的入队申请,申请队名:" + noticeDto.getTeamName();

        notice.setTitle(title1);
        notice.setContent(content1_title1);
        notice.setConfirm(0);
        return noticeMapper.insert(notice);
    }
}
